<template>
	<view class="u-page">



		<u-collapse :value="[]">
			<u-collapse-item name="1" title="工程信息" icon="https://cdn.uviewui.com/uview/example/cell.png">
		
		<buildinfo :buildid='buildid'></buildinfo>
		</u-collapse-item></u-collapse>

		<u-list>
			<u-list-item class="u-demo-block" v-for="(i,index) in buildlistshow" :key="index">
				<!-- <text class="u-demo-block__title">多图模式</text> -->

				<u-line color="#2979ff"></u-line>
				<u-gap height="20"></u-gap>
				<view class="u-demo-block__content">
					<view class="album">
						<view class="album__avatar">
							<image src="/static/uview/common/logo.png" mode="" style="width: 32px;height: 32px;">
							</image>
						</view>
						<view class="album__content">
							<u--text :text="i.date" type="primary" bold size="17"></u--text>
							<!-- 				<u--text margin="0 0 8px 0" type="info" size="13"
								:text="'施工负责人:'+build.ProjManager+' '+(build.ProjManager2||'')"></u--text>
							<u-gap height="20"></u-gap> -->

							<view>
								<u--text margin="0 0 8px 0" type="warning" text="安全交底"></u--text>
								<view v-for="(safe,index1) in i.data.BuildSafeList" :key="index1">
									<u-button type="primary" customStyle="width:100px" size="mini" shape="circle"
										@click="gosafeview(safe.Id)" text="查看详情"></u-button>
									<u--text margin="0 0 8px 0" type="info" size="13" :text="'提交时间：'+safe.CreateTime">
									</u--text>
									<u--text margin="0 0 8px 0" type="info" size="13" prefixIcon="map" :text="safe.Addr"
										@click="gomap(safe.Latlng)">
									</u--text>
									<!-- 			<u--text margin="0 0 8px 0" type="info" text="安全交底文件照:">
								</u--text>
								<u-album :urls="safe.urls1"></u-album> -->
									<u--text margin="0 0 8px 0" type="info" text="人员照:">
									</u--text>
									<u-album :urls="geturls(safe.FileList, '人员照片')"></u-album>
									<u--text margin="0 0 8px 0" type="info" text="现场照:">
									</u--text>
									<u-album :urls="geturls(safe.FileList, '现场照片')"></u-album>
								</view>

								<u-gap height="20"></u-gap>
								<u-line color="#2979ff"></u-line>
								<u-gap height="20"></u-gap>
								<u--text margin="0 0 8px 0" type="warning" text="施工日志"></u--text>
								<view v-for="(daily,index2) in i.data.BuildDailyList">
									<u-button type="primary" size="mini" customStyle="width:100px" shape="circle"
										@click="godailyview(daily.Id)" text="查看详情"></u-button>
									<u--text margin="0 0 8px 0" type="info" size="13" :text="'提交时间：'+daily.CreateTime">
									</u--text>
									<u--text margin="0 0 8px 0" type="info" prefixIcon="map" size="13"
										:text="daily.Addr" @click="gomap(daily.Latlng)">
									</u--text>
									<u--text margin="0 0 8px 0" type="info" text="工程施工日志照片:">
									</u--text>
									<u-album :urls="geturls(daily.FileList, '工程施工日志照片')"></u-album>
									<u--text margin="0 0 8px 0" type="info" text="主要施工环节与隐蔽工程照片:">
									</u--text>
									<u-album :urls="geturls(daily.FileList, '主要施工环节与隐蔽工程照片')"></u-album>
								</view>

								<u-gap height="20"></u-gap>
								<u-line color="#2979ff"></u-line>
								<u-gap height="20"></u-gap>
								<u--text margin="0 0 8px 0" type="warning" text="交底检查"></u--text>
								<view v-for="(check,index3) in i.data.BuildSafeCheckList">
									<u--text margin="0 0 8px 0" type="info" size="13" :text="'检查时间：'+check.CreateTime">
									</u--text>
									<u--text margin="0 0 8px 0" type="info" size="13" :text="'检查人：'+check.Creator">
									</u--text>
									<u--text margin="0 0 8px 0" type="info" size="13" :text="'检查结果：'+check.Result">
									</u--text>
									<u--text margin="0 0 8px 0" type="info" size="13" :text="'存在问题：'+check.Tag">
									</u--text>
									<u--text margin="0 0 8px 0" type="info" size="13" :text="'备注：'+check.Remark">
									</u--text>
									<u-gap height="20"></u-gap>
								</view>
								<u-gap height="20"></u-gap>
								<u-line color="#2979ff"></u-line>
								<u-gap height="20"></u-gap>

				<!-- 				<u--text margin="0 0 8px 0" type="primary" prefixIcon="plus-circle" text="添加安全交底"
									v-if="cansafe(build)" @click="gosafe(build.Id)">
								</u--text>
								<u--text margin="0 0 8px 0" type="primary" prefixIcon="edit-pen" text="添加工程日志"
									v-if="cansafe(build)" @click="godaily(build.Id)">
								</u--text>
								<u--text margin="0 0 8px 0" type="primary" prefixIcon="share-square" text="分派"
									v-if="user.Id==build.ProjManagerId" @click="changesend(build)">
								</u--text> -->
							</view>
				<!-- 			<u--text margin="0 0 8px 0" type="primary" prefixIcon="chat" text="添加工程检查"
								v-if="!user.Roles || user.Roles.indexOf('管理员角色')>-1 || user.Roles.indexOf('项目经理')>-1 "
								@click="gocheck(build.Id)">
							</u--text> -->


						</view>
					</view>
				</view>
			</u-list-item>
			<!-- <u-loadmore :status="status" /> -->
		</u-list>
		<u-back-top :scroll-top="scrollTop"></u-back-top>
		<!-- <u-loading-page :loading="loading" bg-color='rgba(0, 0, 0, 0.3)'></u-loading-page> -->
		<u-picker :loading="changeloading" @confirm="setChange" @cancel="showchange=false" :show="showchange"
			:columns="changelist" keyName="Name"></u-picker>
	</view>
</template>

<script>
	import {
		baseUrl
	} from '@/common/config.js'
	import coordtransform from 'coordtransform'
	import {

		CountByDay,
		ListByDay,
		getbuildlist,
		getbuildsafebyid,
		getfilebyoutid,
		GetChangeSendList,
		updatebuild,
		ChangeSend,
		ListByBuild,
	} from '@/common/api.js'
	import buildinfo from "./c_buildinfo.vue"
	export default {
		components: {
		  buildinfo
		},
		data() {
			return {
				pageSize: 10,
				status: 'loadmore',
				changeloading: false,
				showchange: false,
				changelist: [],
				scrollTop: 0,
				currentpage: 1,
				nomore: false,
				user: {},
				loading: false,
				buildlistshow: [],
				showBuildFilter: false,
				parameters: [{
					FieldName: "Name",
					FieldValue: "",
					ConditionalType: "Like"
				}, {
					FieldName: "No",
					FieldValue: "",
					ConditionalType: "Like"
				}, {
					FieldName: "ProjManager",
					FieldValue: "",
					ConditionalType: "Like"
				}, ],
				currentdate: this.getNowFormatDate(),
				show: false,
				build: {},

				buildid: "",
				user: JSON.parse(uni.getStorageSync("$userinfo")),
			}
		},
		onLoad(opt) {
			this.buildid = opt.buildid

			this.buildlistshow = []
			this.init()
		},

		onPageScroll(e) {
			this.scrollTop = e.scrollTop;
		},
		onShow() {
			// if (uni.getStorageSync('changed') == "1")
			// if(true)
			// {

			// this.getlist()
			// console.log("变了")
			// this.user = JSON.parse(uni.getStorageSync("$userinfo"))
			if (uni.getStorageSync("changed") == "1") {
				this.buildlistshow = []
				this.init()
			}
			uni.setStorageSync('changed', "0");
			// }
		},

		methods: {
			geturls(flist, type) {
				return flist.filter(e => e.MyType == type).reduce((sum, cur) => {
					sum.push(`${baseUrl}/SysFile/download?id=${cur.Id}`)
					return sum
				}, [])

			},
			cansafe(build) {
				return true
				// if (this.user.Roles.indexOf('施工负责人角色') > -1)
				// 	return (this.user.Id == build.ProjManagerId || this.user.Id == build.ProjManagerId2) && this
				// 		.currentdate == this.getNowFormatDate()
				// if (this.user.Roles.indexOf('合作单位负责人') > -1)
				// 	return (this.user.Id == build.ProjManagerId || this.user.Id == build.ProjManagerId2 || this.user.Dept
				// 		.Name == build.SecondUnit) && this.currentdate == this.getNowFormatDate()
				// return false
			},
			setChange(e) {
				this.currentbuild.ProjManager2 = e.value[0].Name
				this.currentbuild.ProjManagerId2 = e.value[0].Id
				updatebuild(this.currentbuild).then(res => {
					this.init();
					this.showchange = false;
				})
			},
			changesend(build) {
				this.currentbuild = build
				this.changeloading = true
				this.showchange = true
				let unit = build.SecondUnit
				if (build.WorkCompany) //劳务的情况
				{
					unit = build.ProjManager
				}
				GetChangeSendList(unit).then(res => {
					this.changelist = [res]
					this.changeloading = false
				})

			},
			gocheck(buildid) {
				uni.navigateTo({
					url: `/pages/index/buildsafecheck?buildid=${buildid}&targetdate=${this.currentdate}`
				})
			},
			gocount() {
				uni.navigateTo({
					url: `/pages/index/buildcount?targetdate=${this.currentdate}`
				})
			},
			scrolltolower() {
				if (this.nomore == true)
					return
				this.currentpage++;
				this.loading = true
				this.init()

			},
			init() {
				this.loading = true



				let p = [...this.parameters]
				// p.push(userfilter)
				var para = {
					// "jsonPara": JSON.stringify(p.filter(e => e.FieldValue != "")),
					"Parameters": p.filter(e => e.FieldValue != ""),
					"orderBys": [
						"CreateTime desc"
					],
					"pageSize": this.pageSize,
					"pageIndex": this.currentpage,
				}
				this.status = "loading"
				ListByBuild(this.buildid).then(res => {
					var buildlist=[]
					for(var i in res)
					{
						buildlist.push({
							date:i,
							data:res[i]
						})
					}
					this.buildlistshow = buildlist.sort((a,b)=>b.date>a.date?1:-1)
				})

			},
			getNowFormatDate() {
				var date = new Date();

				var year = date.getFullYear();
				var month = date.getMonth() + 1;
				var d = date.getDate();
				var hour = date.getHours();
				var minute = date.getMinutes();
				var second = date.getSeconds();

				if (month < 10) {
					month = "0" + month;
				}

				if (d < 10) {
					d = "0" + d;
				}

				if (hour < 10) {
					hour = "0" + hour;
				}

				if (minute < 10) {
					minute = "0" + hour;
				}

				if (second < 10) {
					second = "0" + second;
				}

				return year + "-" + month + "-" + d //+ " " +hour + ":" + minute + ":" + second;
			},
			confirm(e) {
				this.buildlistshow = []
				this.currentdate = e[0]
				this.show = false
				this.init()

			},
			gomap(Latlon) {
				if (Latlon) {
					let latitude = parseFloat(Latlon.split(',')[0])
					let longitude = parseFloat(Latlon.split(',')[1])
					var lola = coordtransform.wgs84togcj02(longitude, latitude)
					uni.openLocation({
						latitude: lola[1],
						longitude: lola[0],
						success: function() {
							console.log('success');
						}
					});
				}
			},
			changeHandler(e) {
				const {
					columnIndex,
					value,
					values, // values为当前变化列的数组内容
					index,
					// 微信小程序无法将picker实例传出来，只能通过ref操作
					picker = this.$refs.uPicker
				} = e
				this.showBuildFilter = false
				// 当第一列值发生变化时，变化第二列(后一列)对应的选项
				// if (columnIndex === 0) {
				//     // picker为选择器this实例，变化第二列对应的选项
				//     picker.setColumnValues(1, this.columnData[index])
				// }
			},
			gosafe(id) {
				uni.navigateTo({
					url: "/pages/index/buildsafedetail?buildid=" + id
				})
			},
			godaily(id) {
				uni.navigateTo({
					url: "/pages/index/builddailydetail?buildid=" + id
				})
			},
			gosafeview(id) {
				uni.navigateTo({
					url: "/pages/index/buildsafeView?id=" + id
				})
			},
			godailyview(id) {
				uni.navigateTo({
					url: "/pages/index/builddailyView?id=" + id
				})
			},
		}
	}
</script>

<style lang="scss">
	.swiper {
		height: 220px;
	}

	.grid-text {
		font-size: 14px;
		color: #909399;
		padding: 10rpx 0 20rpx 0rpx;
		/* #ifndef APP-PLUS */
		box-sizing: border-box;
		/* #endif */
	}

	.index-content {
		background-color: #f3f4f6;
		min-height: 100vh;
	}

	.index-block {
		padding: 40rpx;
		background-color: #fff;
	}

	.index-block-title {
		font-size: 40rpx;
		font-weight: bold;
		padding: 0 0 40rpx 0;
	}

	.item-tj {

		width: 103%;
		height: 160rpx;
		border-radius: 16rpx;

		&-frist {
			background-color: rgba($color: #2979ff, $alpha: 0.8);
			// background-image: url('/static/img/bg/qb.png');
		}

		&-second {
			background-color: rgba($color: #303133, $alpha: 0.8);
			// background-image: url('/static/img/bg/qb.png');
		}

		&-thrid {
			background-color: rgba($color: #19be6b, $alpha: 0.8);
			// background-image: url('/static/img/bg/qb.png');
		}
	}

	.detail_list {
		height: 700rpx;
		overflow: auto;
		color: #9E9E9E;

		.detail_item {
			display: flex;
			flex-direction: row;
			margin: 20rpx 0;
			align-items: center;

			.icon {
				width: 30%;
				text-align: center;

				li {
					font-size: 80rpx;
				}
			}

			.right_content {
				width: 50%;
				text-align: center;
			}

			.icon-income {
				color: #4AABF9;
			}

			.icon-expend {
				color: #E45521;
			}

			.money {
				color: #000;
			}
		}
	}

	.album {
		@include flex;
		align-items: flex-start;

		&__avatar {
			background-color: $u-bg-color;
			padding: 5px;
			border-radius: 3px;
		}

		&__content {
			margin-left: 10px;
			flex: 1;
		}
	}
</style>